package com.fengba.nanzhi.tabbar01;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.utils.Function;
import com.fengba.nanzhi.utils.OKManager;
import com.fengba.nanzhi.utils.Preferences;

import org.json.JSONException;
import org.json.JSONObject;

import de.hdodenhof.circleimageview.CircleImageView;

public class FX_SQNumber extends Activity {

    private String account;
    private String headimg;
    private String level_id;
    private String giveid;
    private String type;

    private TextView SQNtv01;
    private TextView SQNet01;
    private CircleImageView SQNiv01;
    private FrameLayout SQNfl01;
    private Button SQNbtn01;
    private TextView SQNtv02;

    public FX_SQNumber() {

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fx_sq_number);
        Intent intent = getIntent();
        account = intent.getStringExtra("account");
        headimg = intent.getStringExtra("headimg");
        level_id = intent.getStringExtra("level_id");
        giveid = intent.getStringExtra("giveid");
        type = intent.getStringExtra("type");
        SQNtv01 = findViewById(R.id.SQNtv01);
        SQNtv01.setText(account);
        SQNiv01 = findViewById(R.id.SQNiv01);
        Glide.with(FX_SQNumber.this).load(headimg).into(SQNiv01);
        SQNfl01 = findViewById(R.id.SQNfl01);
        createStart(Integer.parseInt(level_id));




        SQNtv02 = findViewById(R.id.SQNtv02);
        SQNet01 = findViewById(R.id.SQNet01);
        SQNet01.setInputType(EditorInfo.TYPE_CLASS_PHONE);
        /**
         * 返回
         */
        ImageButton SQNbtn02 = (ImageButton)findViewById(R.id.SQNbtn02);
        SQNbtn02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        /**
         * 提交
         */
        SQNbtn01 = (Button)findViewById(R.id.SQNbtn01);
        SQNbtn01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (type.equals("2"))
                {
                    init_();
                }else
                 {
                  init();
                 }

            }
        });

        if (type.equals("2"))
        {
            SQNbtn01.setText("确认赠送");
            SQNtv02.setText("赠送数量");
        }

    }//都在这里打

    /**
     * 生成星星
     * @param index
     */
    private void createStart(int index){
        for (int i = 0; i < 4; i++){
            ImageView img = new ImageView(FX_SQNumber.this);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(0,0);
            params.width = Function.dip2px(FX_SQNumber.this,22);
            params.height = Function.dip2px(FX_SQNumber.this,22);
            params.leftMargin = 4+i*Function.dip2px(FX_SQNumber.this,30);
            img.setLayoutParams(params);
            img.setImageDrawable(ContextCompat.getDrawable(FX_SQNumber.this.getApplicationContext(),R.mipmap.img_0022));
            SQNfl01.addView(img);
        }

        for (int i = 0; i < index; i++){
            ImageView img = new ImageView(FX_SQNumber.this);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(0,0);
            params.width = Function.dip2px(FX_SQNumber.this,22);
            params.height = Function.dip2px(FX_SQNumber.this,22);
            params.leftMargin = 4+i*Function.dip2px(FX_SQNumber.this,30);
            img.setLayoutParams(params);
            img.setImageDrawable(ContextCompat.getDrawable(FX_SQNumber.this.getApplicationContext(),R.mipmap.img_0023));
            SQNfl01.addView(img);
        }
    }




    /**
     * 隐藏软键盘
     *
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    public void init()
    {
        String id = null;
        try {
            Object user_info = Preferences.getParam(FX_SQNumber.this, "user_info", "");
            JSONObject myJson = null;
            myJson = new JSONObject(user_info.toString());
            id = myJson.get("id").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OKManager.getInstance().type2008(giveid, id,String.valueOf(SQNet01.getText()), new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if (jsonObject.get("code").equals(0)) {
                        Toast.makeText(FX_SQNumber.this, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                        new Handler().postDelayed(new Runnable(){
                            public void run() {
                                finish();
                            }
                        }, 150);
                    } else {
                        //jsonArray1 = jsonObject.getJSONArray("data");
                        Toast.makeText(FX_SQNumber.this, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            @Override
            public void onFailure(String string){
                Toast.makeText(FX_SQNumber.this, "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void init_()
    {
        String id_ = null;
        try {
            Object user_info = Preferences.getParam(FX_SQNumber.this, "user_info", "");
            JSONObject myJson = null;
            myJson = new JSONObject(user_info.toString());
            id_ = myJson.get("id").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OKManager.getInstance().type2013(giveid,id_,String.valueOf(SQNet01.getText()), new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if (jsonObject.get("code").equals(0)) {
                        Toast.makeText(FX_SQNumber.this, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                        new Handler().postDelayed(new Runnable(){
                            public void run() {
                                finish();
                            }
                        }, 150);
                    } else {
                        Toast.makeText(FX_SQNumber.this, jsonObject.get("message").toString(), Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //refreshLayout.finishRefresh();
            }
            @Override
            public void onFailure(String string){
                Toast.makeText(FX_SQNumber.this, "网络错误", Toast.LENGTH_SHORT).show();
                //refreshLayout.finishRefresh();
            }
        });
    }
}

